#include "WiFly.h"
#include "Credentials.h"

Server server(1919); //ouverture du port 1919
int flex = 0;

void setup() {
  WiFly.begin();
  WiFly.enableDHCP();
  if (!WiFly.join(ssid, passphrase,true)) {
    while (1) {
      // Hang on failure.
    }
  }

  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());

  server.begin();
  delay(1000);
}

void loop() {
  Client client = server.available();
  if (client) {
    if (client.connected()) {
      while(client.available() < 1);
      char c1 = client.read();
      while(client.available() < 1);
      char c2 = client.read();

      if(c1=='#' && c2=='3'){
	client.print("DOO ");
	int val = analogRead(flex);
	if(val >= 276) //276 etant la valeur moyenne du flex sensor. Une valeur superieur implique une torsion du flex sensor
	  Serial.println("Fermee");
	else
	  Serial.println("Ouvert");
      }
      if(c1=='#' && c2=='?'){
        client.println("OK DOO");
      }
      
      client.stop();
    }
    delay(100);
  }
}
